﻿using System.Text;
using System.Net;
using System.IO;
using Newtonsoft.Json;

namespace GrainManage.Proxy.Util
{
    public class WebUtil
    {
        public static ResultModel ExecuteByPost<ResultModel>(string resource, object inputParams) where ResultModel : class
        {
            var jsonData = RequestByPost(resource, inputParams);
            return ToResult<ResultModel>(jsonData);
        }

        //parameters like:name=john&age=20
        public static ResultModel ExecuteByGet<ResultModel>(string resource, string parameters) where ResultModel : class
        {
            var jsonData = RequestByGet(resource, parameters);
            return ToResult<ResultModel>(jsonData);
        }

        //*****************************************Help Method*****************************************
        private static string RequestByPost(string resource, object inputParams)
        {
            var result = string.Empty;
            var strInput = Serialize(inputParams);
            var byteData = UTF8Encoding.UTF8.GetBytes(strInput);// Create a byte array of the data we want to send

            var request = WebRequest.Create(resource) as HttpWebRequest;
            request.Method = "POST"; // Set type to POST
            request.ContentType = "application/json";
            request.ContentLength = byteData.Length;  // Set the content length in the request headers

            using (var postStream = request.GetRequestStream())
            {
                postStream.Write(byteData, 0, byteData.Length);  // Write data
            }

            using (var response = request.GetResponse() as HttpWebResponse)  // Get response
            {
                using (var stream = response.GetResponseStream())  // Get the response stream
                {
                    using (var reader = new StreamReader(stream))
                    {
                        result = reader.ReadToEnd();
                    }
                }
            }

            return result;
        }

        private static string RequestByGet(string resource, string parameters)
        {
            var result = string.Empty;

            if (!string.IsNullOrEmpty(parameters))
            {
                resource = resource + "?" + parameters;
            }

            var request = WebRequest.Create(resource) as HttpWebRequest;
            request.Method = "GET";   // Set type to GET

            using (var response = request.GetResponse() as HttpWebResponse)
            {
                using (var stream = response.GetResponseStream())
                {
                    using (var reader = new StreamReader(stream))
                    {
                        result = reader.ReadToEnd();
                    }
                }
            }

            return result;
        }

        private static string Serialize(object obj)
        {
            return JsonConvert.SerializeObject(obj);
        }

        private static TModel Deserialize<TModel>(string input)
        {
            return JsonConvert.DeserializeObject<TModel>(input);
        }

        private static ResultModel ToResult<ResultModel>(string jsonData) where ResultModel : class
        {
            ResultModel result = default(ResultModel);
            if (!string.IsNullOrEmpty(jsonData))
            {
                if (typeof(ResultModel) == typeof(string))
                {
                    result = jsonData as ResultModel;
                }
                else
                {
                    result = Deserialize<ResultModel>(jsonData);
                }
            }
            return result;
        }
    }
}
